//
//  HeadView.swift
//
//
//  Created by Apple on 15/12/1.
//
//

import UIKit
import SnapKit
import SDWebImage
class HeadView: UIView {
    var bottomConstraint: Constraint?
    
    var status :statuses?{
        didSet{
            
            //设置数据
            //            print(status)
            iconImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            
            nameLabe.text = status?.user?.name
            vipImage.image = status?.user?.mbrankImage
            bigV.image = status?.user?.verified_type_image
            contentLabe.text = status?.text
            
            layouView.imageURL = status?.imageUrl
    
            //创建实例化对象Constraint 移除constraint
            bottomConstraint?.uninstall()
            //判断imageUrl有没有配图
            if let urls = status?.imageUrl where urls.count != 0{
            
            //有配图的时候
            //设置数据源
            layouView.imageURL = urls
            //设置底部的约束
            self.snp_updateConstraints(closure: { (make) -> Void in
               self.bottomConstraint = make.bottom.equalTo(layouView.snp_bottom).offset(OffsetCell).constraint
            })
               //显示配图
                layouView.hidden = false
            }else{
                //没有配图的时候
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabe.snp_bottom).offset(OffsetCell).constraint
                })
                layouView.hidden = true
            }
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 自动布局
    
    private func setupUI (){
        addSubview(iconImage)
        addSubview(nameLabe)
        addSubview(timeLabe)
        addSubview(originLabe)
        addSubview(contentLabe)
        addSubview(vipImage)
        addSubview(bigV)
        addSubview(layouView)
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(OffsetCell)
            make.size.equalTo(CGSize(width: SizeWidth, height: SizeWidth))
            
            
        }
        nameLabe.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImage.snp_right).offset(OffsetCell)
            make.top.equalTo(iconImage.snp_top)
        }
        timeLabe.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabe.snp_left)
            make.top.equalTo(nameLabe.snp_bottom).offset(OffsetCell)
        }
        originLabe.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabe.snp_right).offset(OffsetCell)
            make.bottom.equalTo(timeLabe.snp_bottom)
        }
        contentLabe.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(OffsetCell)
            make.top.equalTo(iconImage.snp_bottom).offset(OffsetCell)
        }
        vipImage.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabe.snp_right).offset(OffsetCell)
            make.top.equalTo(nameLabe.snp_top)
        }
        bigV.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        layouView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(contentLabe.snp_bottom).offset(OffsetCell)
            make.left.equalTo(contentLabe.snp_left)
            //        make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //设置大小
    self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(layouView.snp_bottom).offset(OffsetCell).constraint
      }
    }
    
    //MARK: 添加子视图
    //头像
    private lazy var iconImage: UIImageView = UIImageView()
    //名字
    private lazy var nameLabe :UILabel = {
        let labe = UILabel()
        labe.text = "马晓辉"
        labe.textColor = UIColor.orangeColor()
        labe.font = UIFont.systemFontOfSize(14)
        labe.sizeToFit()
        return labe
        }()
    //发送时间
    private lazy var timeLabe :UILabel = {
        let labe = UILabel()
        labe.text = "11:11"
        labe.textColor = UIColor.redColor()
        labe.font = UIFont.systemFontOfSize(10)
        labe.sizeToFit()
        return labe
        }()
    
    //发送来源
    private lazy var originLabe :UILabel = {
        let labe = UILabel()
        labe.text = "QQ"
        labe.textColor = UIColor.blackColor()
        labe.font = UIFont.systemFontOfSize(10)
        labe.sizeToFit()
        return labe
        }()
    //微博正文
    private lazy var contentLabe :UILabel = {
        let labe = UILabel()
        labe.text = "不会敲啊不会敲"
        labe.textColor = UIColor.blackColor()
        labe.font = UIFont.systemFontOfSize(14)
        //换行
        labe.numberOfLines = 0
        //设定label的宽度
        labe.preferredMaxLayoutWidth = ScreenW - 2 * OffsetCell
        //自动填充
        labe.sizeToFit()
        return labe
        }()
    //会员的图片
    private lazy var vipImage :UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //V
    private lazy var bigV :UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //配图
    private lazy var layouView :LayouView = LayouView()
    
}
